define(["jquery","bbcare",
	"scprit/assess/app/monthlyAssess/monthlyAssessDetail.ui.js",
	"scprit/assess/app/monthlyAssess/monthlyAssessDetail.servers.js"],function($,$bb,$ui,$servers){
	var event = {};

	/**
	 * 获取指派的评估医生
	 */
	var ajaxAssessDetailInfo2CallBack = function(data,params){
		$.extend(params,{"doctorId":data.doctorId});
		$servers.ajaxDoctorList(params,ajaxWorkorderResultCallBack);
	}

	/**
	 * 提交或修改评估指导意见成功后的回调函数
	 */
	var ajaxManualguideModifyCallBack = function(data,originalParams){
		var type = $servers.getType(originalParams.type,"next");
		var params = {
			"workOrderId":originalParams.workOrderId,
			"type":type
		}
		if( type == "01" || type == "02" || type == "03" || type == "04" || type == "05" ){
			$servers.ajaxWorkorderResult(params,ajaxWorkorderResultCallBack);
		}else if( type == "06"){
			$servers.ajaxAssessDetailInfo(params,ajaxAssessDetailInfo2CallBack);
		}else if( type == "07"){
			ajaxWorkorderResultCallBack({},params);
		}
	}

	/**
	 * 上一步
	 */
	var onPreStep = function(){
		var $dom = $(this).parent();
		var workOrderId = $dom.attr("workOrderId");
		var type = $dom.attr("type");
		var params = {
			"workOrderId":workOrderId,
			"type":$servers.getType(type,"pre")
		}
		$servers.ajaxWorkorderResult(params,ajaxWorkorderResultCallBack);
	}

	/**
	 * 下一步
	 */
	var onNextStep = function(){
		var $dom = $(this).parent();
		var workOrderId = $dom.attr("workOrderId");
		var type = $dom.attr("type");
		var params = {
			"workOrderId":workOrderId,
			"type":type
		}
		if(type == "01" || type == "02" || type == "03"){
			var evaluationId = $dom.attr("evaluationId");
			$.extend(params,{"evaluationId":evaluationId});
		}
		switch(type){
			case "01":
				var growthguide = {
					"weight":$("#weightAnalysis").val(),
					"height":$("#heightAnalysis").val(),
					"head":$("#headAnalysis").val()
				}
				$.extend(params,{"growthguide":growthguide});
				$servers.ajaxManualguideModify(params,ajaxManualguideModifyCallBack);
				break;
			case "02":
				if( $("#analysis02") ){ // data.result存在，即该宝宝做了智力发育评估
					var asqguide = {"suggest":$("#analysis02").val()};
					$.extend(params,{"asqguide":asqguide});
				}else{
					// 此处应该不保存了，直接查询下一个
				}
				$servers.ajaxManualguideModify(params,ajaxManualguideModifyCallBack);
				break;
			case "03":
				if( $("#analysis03") ){ // data.result存在，即该宝宝做了养育环境评估
					var ahemdguide = {"suggest":$("#analysis03").val()};
					$.extend(params,{"ahemdguide":ahemdguide});
				}else{
					// 此处应该不保存了，直接查询下一个
				}
				$servers.ajaxManualguideModify(params,ajaxManualguideModifyCallBack);
				break;
			case "04":
				var healthguide = {
					"behavioral":$("#behavioralAnalysis04").val(),
					"other":$("#otherAnalysis04").val(),
					"vaccinate":$("#vaccinateAnalysis04").val(),
					"feed":$("#feedAnalysis04").val(),
					"supplement":$("#supplementAnalysis04").val(),
				}
				$.extend(params,{"healthguide":healthguide});
				$servers.ajaxManualguideModify(params,ajaxManualguideModifyCallBack);
				break;
			case "05":
				var evaluate = $("#evaluateAnalysis05 li.active").html();
				var allguide = {
					"evaluate":evaluate,
					"suggest":$("#suggestAnalysis05").val(),
				}
				$.extend(params,{"allguide":allguide});
				$servers.ajaxManualguideModify(params,ajaxManualguideModifyCallBack);
				break;
			case "06":
				var doctorId = $("#analysis06 li.active").attr("doctorId");
				if(doctorId){
					$.extend(params,{"doctorId":doctorId});
					$servers.ajaxDoctorModify(params,ajaxManualguideModifyCallBack);
				}else{
					$bb.toastShow("请指派评估医生");
				}
				break;
		}
	}

	/**
	 * 修改
	 */
	var onModifyBtn = function(){
		var $dom = $(this).parent();
		var workOrderId = $dom.attr("workOrderId");
		var params = {
    		"workOrderId":workOrderId, // workOrderId
    		"type":"01"
    	};
    	$servers.ajaxWorkorderResult(params,ajaxWorkorderResultCallBack);
	}
	/**
	 * 完成
	 */
	var onFinishBtn = function(){
		$bb.openPage("monthlyAssessList");
	}

	/**
     *	请求评估记录结果信息(包含指导意见）成功后的回调函数
     */
    var ajaxWorkorderResultCallBack = function(data,params){
    	$.extend(data,params);
    	$ui.fillAssessDataInit({
    		"id":"fillAssessData",
    		"data":data
    	});
    	if(data.type == "05"){
    		$("#evaluateAnalysis05 li").click(function(){
    			$(this).addClass("active").siblings().removeClass("active");
    		});
    	} 
    	if(data.type == "06"){
    		$("#analysis06 li").click(function(){
    			$(this).addClass("active").siblings().removeClass("active");
    		});
    	} 
    	if(data.type == "07"){
	    	$("#modifyBtn").unbind("click");
	    	$("#modifyBtn").click(onModifyBtn);
	    	$("#finishBtn").unbind("click");
	    	$("#finishBtn").click(onFinishBtn);
    	}else{
    		$("#nextStep").unbind("click");
	    	$("#nextStep").click(onNextStep);
	    	$("#preStep").unbind("click");
	    	$("#preStep").click(onPreStep);
    	}
    }
    event.ajaxWorkorderResultCallBack = function(data,params){
    	ajaxWorkorderResultCallBack(data,params);
    }

	return event;
});